package com.curso.sesion10;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Dog implements Serializable {
	transient private Collar theCollar;  
	private int dogSize;  

	public Dog(Collar collar, int size) {  
		theCollar = collar;  
		dogSize =  size;  
	}  

	public Collar getCollar() {  
		return theCollar;  
	}  

	private void writeObject(ObjectOutputStream os) { 
		try {  
			os.defaultWriteObject();  
			os.writeInt(theCollar.getCollarSize());  
		}catch(Exception e) {  
			e.printStackTrace();  
		}  
	}  

	private void readObject(ObjectInputStream is) {  
		try {  
			is.defaultReadObject();  
			theCollar = new Collar(is.readInt());  
		}catch(Exception e) {  
			e.printStackTrace();  
		}  
	}  
}  

class Collar {  
	private int collarSize;  
	public Collar(int size) {  
		collarSize = size;  
	}  
	public int getCollarSize() {  
		return collarSize;  
	}  
}  

public class SerializeDog {  
	public static void main(String[] args) {  
		Collar c = new Collar(3);  
		Dog d = new Dog(c,5);  
		System.out.println("before: collar size is " + d.getCollar().getCollarSize());  
		try {  
			FileOutputStream fs = new FileOutputStream("test.ser");  
			ObjectOutputStream os = new ObjectOutputStream(fs);  
			os.writeObject(d); // line 1--->Dog object getting serialized[/B]  
			os.close();  
		}catch(Exception e) {   
			e.printStackTrace();  
		}  
		try {  
			FileInputStream fis = new FileInputStream("test.ser");  
			ObjectInputStream ois = new ObjectInputStream(fis);  
			d= (Dog)ois.readObject(); // line 2---> Dog object getting deserialized[/B]  
			ois.close();  
		}catch(Exception e) {  
			e.printStackTrace();  
		}  
		System.out.println("after: collar size is " + d.getCollar().getCollarSize());  
	}  
}  